package cn.com.cfae.iras.doc.analyze.components.commonsense;

import cn.com.cfae.iras.doc.analyze.components.BaseComponent;
import cn.com.cfae.iras.doc.analyze.components.beans.ComponentResult;
import cn.com.cfae.iras.doc.analyze.components.consistency.U1004;
import cn.com.cfae.iras.doc.analyze.parse.kv.KVText;
import cn.com.cfae.iras.doc.analyze.parse.kv.TextLayout;
import cn.com.cfae.iras.doc.analyze.parse.model.ChapterModel;
import cn.com.cfae.iras.doc.analyze.parse.model.DocumentModel;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by zouxun on 2018/10/29
 */
@Component("C1002")
@Scope("prototype")
public class C1002 extends BaseComponent {
    private static Logger logger = LoggerFactory.getLogger(C1002.class);


    @Autowired
    U1004 u1004;

    /**
     * @desc 兑付日：XXXX年XX月XX日=起息日+发行期限，“时间计算按照算头不算尾原则”（如兑付日遇法定节假日，则顺延至下一工作日）。
     * 抓取“起息日”和“发行期限”后面的数据，计算理论兑付日，忽略节假日
     *
     * @return
     */
    @Override
    public List<ComponentResult> handlerM() {
        logger.info("开始调用C1002组件，处理M表...............");
        List<ComponentResult> componentResults = ruleCheck();
        return componentResults;
    }

    @Override
    public List<ComponentResult> handlerBM() {
        logger.info("开始调用C1002组件，处理BM表...............");
        List<ComponentResult> componentResults = ruleCheck();
        return componentResults;
    }

    @Override
    public List<ComponentResult> handlerDM() {
        logger.info("开始调用C1002组件，处理DM表...............");
        List<ComponentResult> componentResults = ruleCheck();
        return componentResults;
    }

    @Override
    public List<ComponentResult> handlerBDM() {
        logger.info("开始调用C1002组件，处理BDM表...............");
        List<ComponentResult> componentResults = ruleCheck();
        return componentResults;
    }

    private List<ComponentResult> ruleCheck(){
        DocumentModel documentModel = super.getExtractData();

        List<ComponentResult> componentResults = new ArrayList<>();

        String keyword="兑付日：XXXX年XX月XX日=起息日+发行期限，“时间计算按照算头不算尾原则”（如兑付日遇法定节假日，则顺延至下一工作日）。抓取“起息日”和“发行期限”后面的数据，计算理论兑付日，忽略节假日";

        ComponentResult componentResult = u1004.getComponentResult(getCatalogId(),getChapterId(),getRuleTitle(),keyword,"",true,getRegulationId(),1,null,"03");

        ComponentResult qxrComponentResult = u1004.getComponentResult(getCatalogId(),getChapterId(),getRuleTitle(),getCatalogId().concat("起息日"),"",true,getRegulationId(),1,null,"03");

        ComponentResult fxqxComponentResult = u1004.getComponentResult(getCatalogId(),getChapterId(),getRuleTitle(),getCatalogId().concat("发行期限"),"",true,getRegulationId(),1,null,"03");

        try{
            ChapterModel chapterModel = documentModel.getChapterModel(getCatalogId());

            if(chapterModel!=null){
                qxrComponentResult.setPageNum(chapterModel.getStartPage());
                fxqxComponentResult.setPageNum(chapterModel.getStartPage());
                componentResult.setPageNum(chapterModel.getStartPage());

                //兑付日
                KVText dfrKVTextValue=getKVTextValue(getCatalogId(),"FXTK","DFR");

                KVText dfrKVTextKey=getKVTextKey(getCatalogId(),"FXTK","DFR");

                //起息日
                KVText qxrKVTextValue=getKVTextValue(getCatalogId(),"FXTK","QXR");

                KVText qxrKVTextKey=getKVTextKey(getCatalogId(),"FXTK","QXR");

                //发行期限
                KVText fxqxKVTextValue=getKVTextValue(getCatalogId(),"FXTK","FXQX");

                KVText fxqxKVTextKey=getKVTextKey(getCatalogId(),"FXTK","FXQX");


                if(qxrKVTextValue!=null && fxqxKVTextValue!=null){
                    qxrComponentResult.setPageNum(qxrKVTextValue.getPageNumber());
                    fxqxComponentResult.setPageNum(fxqxKVTextValue.getPageNumber());

                    if(qxrKVTextKey!=null){
                        qxrComponentResult=setTextPosition(qxrKVTextKey,qxrComponentResult);
                    }

                    if(fxqxKVTextKey!=null){
                        fxqxComponentResult=setTextPosition(fxqxKVTextKey,fxqxComponentResult);
                    }

                    String qxrText = qxrKVTextValue.getText();
                    logger.info("抽取的起息日为：{}，页码为：{}",qxrText,qxrKVTextValue.getPageNumber());
                    String fxqxText = fxqxKVTextValue.getText();
                    logger.info("抽取的发行期限为：{}，页码为：{}",fxqxText,fxqxKVTextValue.getPageNumber());

                    if(StringUtils.isNotEmpty(qxrText) && StringUtils.isNotEmpty(fxqxText)){
                        Calendar qxrCalendar = dealDate(qxrText);

                        if(qxrCalendar!=null){
                            qxrComponentResult.setPromptMessage("已识别起息日：".concat(qxrText));
                            qxrComponentResult.setAnnotationFlag(false);

                            Map<String,String> releaseDateMap=getReleaseDate(fxqxText);

                            String releaseDateFlag=releaseDateMap.get("flag");

                            if(!releaseDateFlag.equals("false")){
                                if(StringUtils.isNotEmpty(releaseDateMap.get("value"))){
                                    if(releaseDateFlag.equals("year")){
                                        logger.info("发行期限：单位：年，值：{}",releaseDateMap.get("value"));
                                        qxrCalendar.add(Calendar.YEAR,Integer.parseInt(releaseDateMap.get("value")));
                                    }else if(releaseDateFlag.equals("day")){
                                        logger.info("发行期限：单位：天，值：{}",releaseDateMap.get("value"));
                                        qxrCalendar.add(Calendar.DATE,Integer.parseInt(releaseDateMap.get("value")));
                                    }

                                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

                                    String theoreticalValue = simpleDateFormat.format(qxrCalendar.getTime());
                                    logger.info("理论起息日为：{}",theoreticalValue);

                                    fxqxComponentResult.setAnnotationFlag(false);
                                    fxqxComponentResult.setPromptMessage("已识别发行期限：".concat(fxqxText));

                                    if(dfrKVTextValue!=null){
                                        componentResult = setTextPosition(dfrKVTextKey,componentResult);

                                        String dfrText = dfrKVTextValue.getText();
                                        logger.info("兑付日：{}",dfrText);

                                        if(StringUtils.isNotEmpty(dfrText)){
                                            Calendar dfrCalendar = dealDate(dfrText);
                                            if(dfrCalendar!=null){
                                                String qxrTemp=simpleDateFormat.format(qxrCalendar.getTime());
                                                logger.info("理论兑付日:{}",qxrTemp);
                                                String dfrTemp=simpleDateFormat.format(dfrCalendar.getTime());
                                                logger.info("兑付日:{}",dfrTemp);

                                                if(qxrTemp.equals(dfrTemp)){
                                                    logger.info("与理论兑付日对比一致");
                                                    componentResult.setPromptMessage("兑付日与系统计算的理论兑付日对比一致，系统计算的理论兑付日为：".concat(theoreticalValue));
                                                    componentResult.setAnnotationFlag(false);
                                                }else{
                                                    logger.info("与理论兑付日对比不一致");
                                                    componentResult.setPromptMessage("兑付日与系统计算的理论兑付日对比不一致，请核对兑付日，系统计算的理论兑付日为：".concat(theoreticalValue));
                                                    componentResult.setExist("02");
                                                    componentResult.setAnnotationFlag(false);

                                                    qxrComponentResult.setExist("02");
                                                    fxqxComponentResult.setExist("02");
                                                }
                                            }else{
                                                componentResult.setPromptMessage("未识别兑付日，建议人工核对，系统计算的理论兑付日为：".concat(theoreticalValue));
                                                componentResult.setAnnotationFlag(false);
                                            }

                                        }else{
                                            componentResult.setPromptMessage("未识别兑付日，建议人工核对，系统计算的理论兑付日为：".concat(theoreticalValue));
                                            componentResult.setAnnotationFlag(false);
                                        }
                                    }else{
                                        componentResult.setPromptMessage("未识别兑付日，建议人工核对，系统计算的理论兑付日为：".concat(theoreticalValue));
                                        componentResult.setAnnotationFlag(false);
                                    }
                                }else{
                                    fxqxComponentResult.setPromptMessage("未识别发行期限");
                                    fxqxComponentResult.setAnnotationFlag(false);
                                    componentResult.setPromptMessage("未识别发行期限，无法计算理论兑付日，建议人工核对");
                                    if(dfrKVTextKey!=null){
                                        componentResult = setTextPosition(dfrKVTextKey,componentResult);
                                    }
                                }


                            }else{
                                fxqxComponentResult.setPromptMessage("未识别发行期限");
                                fxqxComponentResult.setAnnotationFlag(false);
                                componentResult.setPromptMessage("未识别发行期限，无法计算理论兑付日，建议人工核对");
                                if(dfrKVTextKey!=null){
                                    componentResult = setTextPosition(dfrKVTextKey,componentResult);
                                }
                            }

                        }else{
                            qxrComponentResult.setPromptMessage("起息日数据缺失：".concat(qxrText));
                            fxqxComponentResult.setPromptMessage("已识别发行期限：".concat(fxqxText));
                            componentResult.setPromptMessage("起息日数据缺失，无法计算理论兑付日");
                            if(dfrKVTextKey!=null){
                                componentResult = setTextPosition(dfrKVTextKey,componentResult);
                            }
                        }

                        componentResults.add(qxrComponentResult);
                        componentResults.add(fxqxComponentResult);
                        componentResults.add(componentResult);

                    }else{
                        if(StringUtils.isNotEmpty(qxrText)){
                            Calendar qxrCalendar = dealDate(qxrText);
                            if(qxrCalendar!=null){
                                qxrComponentResult.setPromptMessage("已识别起息日：".concat(qxrText));
                            }else{
                                qxrComponentResult.setPromptMessage("起息日数据缺失：".concat(qxrText));
                            }
                            qxrComponentResult.setAnnotationFlag(false);
                        }else{
                            qxrComponentResult.setPromptMessage("未识别起息日");
                            if(qxrComponentResult.getPageNum()==0){
                                if(chapterModel!=null){
                                    qxrComponentResult.setPageNum(chapterModel.getStartPage());
                                }
                            }
                            componentResult.setPromptMessage("未识别起息日，无法计算，建议人工核对");
                            if(dfrKVTextKey!=null){
                                componentResult = setTextPosition(dfrKVTextKey,componentResult);
                            }
                        }

                        if(StringUtils.isNotEmpty(fxqxText)){
                            fxqxComponentResult.setPromptMessage("已识别发行期限：".concat(fxqxText));
                            fxqxComponentResult.setAnnotationFlag(false);
                        }else{
                            fxqxComponentResult.setPromptMessage("未识别发行期限");
                            if(fxqxComponentResult.getPageNum()==0){
                                if(chapterModel!=null){
                                    fxqxComponentResult.setPageNum(chapterModel.getStartPage());
                                }
                            }
                            componentResult.setPromptMessage("未识别发行期限，无法计算，建议人工核对");
                            if(dfrKVTextKey!=null){
                                componentResult = setTextPosition(dfrKVTextKey,componentResult);
                            }
                        }

                        componentResults.add(qxrComponentResult);
                        componentResults.add(fxqxComponentResult);
                        componentResults.add(componentResult);
                        return componentResults;
                    }

                }else{
                    if(qxrKVTextValue!=null){
                        qxrComponentResult.setPageNum(qxrKVTextKey.getPageNumber());
                        if(StringUtils.isNotEmpty(qxrKVTextValue.getText())){
                            Calendar qxrCalendar = dealDate(qxrKVTextValue.getText());
                            if(qxrCalendar!=null){
                                qxrComponentResult.setPromptMessage("已识别起息日：".concat(qxrKVTextValue.getText()));
                            }else{
                                qxrComponentResult.setPromptMessage("起息日数据缺失：".concat(qxrKVTextValue.getText()));
                            }
                        }else{
                            qxrComponentResult.setPromptMessage("已识别起息日");
                        }

                        qxrComponentResult.setAnnotationFlag(false);

                        if(qxrKVTextKey!=null){
                            qxrComponentResult=setTextPosition(qxrKVTextKey,qxrComponentResult);
                        }
                    }else{
                        qxrComponentResult.setPromptMessage("未识别起息日");

                        if(qxrKVTextKey!=null){
                            qxrComponentResult=setTextPosition(qxrKVTextKey,qxrComponentResult);
                        }

                        if(qxrComponentResult.getPageNum()==0){
                            if(chapterModel!=null){
                                qxrComponentResult.setPageNum(chapterModel.getStartPage());
                            }
                        }

                        componentResult.setPromptMessage("未识别起息日，无法计算，建议人工核对");
                        if(dfrKVTextKey!=null){
                            componentResult = setTextPosition(dfrKVTextKey,componentResult);
                        }
                    }

                    if(fxqxKVTextValue!=null){
                        fxqxComponentResult.setPageNum(fxqxComponentResult.getPageNum());
                        if(StringUtils.isNotEmpty(fxqxKVTextValue.getText())){
                            fxqxComponentResult.setPromptMessage("已识别发行期限：".concat(fxqxKVTextValue.getText()));
                        }else{
                            fxqxComponentResult.setPromptMessage("已识别发行期限");
                        }

                        fxqxComponentResult.setAnnotationFlag(false);

                        if(fxqxKVTextKey!=null){
                            fxqxComponentResult=setTextPosition(fxqxKVTextKey,fxqxComponentResult);
                        }
                    }else{
                        fxqxComponentResult.setPromptMessage("未识别发行期限");
                        if(chapterModel!=null){
                            fxqxComponentResult.setPageNum(chapterModel.getStartPage());
                        }

                        if(fxqxComponentResult.getPageNum()==0){
                            if(chapterModel!=null){
                                fxqxComponentResult.setPageNum(chapterModel.getStartPage());
                            }
                        }

                        componentResult.setPromptMessage("未识别发行期限，无法计算，建议人工核对");
                        if(dfrKVTextKey!=null){
                            componentResult = setTextPosition(dfrKVTextKey,componentResult);
                        }
                    }

                    componentResults.add(qxrComponentResult);
                    componentResults.add(fxqxComponentResult);
                    componentResults.add(componentResult);
                    return componentResults;
                }
            }else{
                componentResult.setPromptMessage("未识别".concat(getCatalogId()).concat("数据，建议人工核对"));
                componentResults.add(componentResult);
                return componentResults;
            }

        }catch (Exception e){
            logger.error(e.getMessage(),e);
        }

        return componentResults;
    }


    public ComponentResult setTextPosition(KVText kvText,ComponentResult componentResult){
        List<ComponentResult.TextPosition> textPositionList = new ArrayList<>();
        ComponentResult.TextPosition textPosition = new ComponentResult().new TextPosition();
        TextLayout textLayout = kvText.getTextLayout();
        if(textLayout!=null){
            textPosition.setX(textLayout.getLeft());
            textPosition.setY(textLayout.getTop());
            textPosition.setContent(textLayout.getText());
            textPositionList.add(textPosition);
            componentResult.setPageNum(textLayout.getPageNumber());
        }
        componentResult.setTextPosition(textPositionList);
        return componentResult;
    }

    //数字大写转小写
    private String convertNumber(String text){
        String result = "";

        Map<String,Integer> dataMap = new HashMap<>();
        dataMap.put("一",1);
        dataMap.put("二",2);
        dataMap.put("三",3);
        dataMap.put("四",4);
        dataMap.put("五",5);
        dataMap.put("六",6);
        dataMap.put("七",7);
        dataMap.put("八",8);
        dataMap.put("九",9);
        dataMap.put("十",10);

        logger.info("数字大写格式：{}",text);

        String regex = "[一|二|三|四|五|六|七|八|九]百([一|二|三|四|五|六|七|八|九]十){0,1}[一|二|三|四|五|六|七|八|九]{0,1}";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(text);

        if(matcher.matches()){
            //百位数
            String hundredDigit = "";
            //十位数
            String tenDigit = "";
            //个位数
            String singleDigit = "";

            regex = "[一|二|三|四|五|六|七|八|九]{1}百";
            pattern = Pattern.compile(regex);
            matcher = pattern.matcher(text);
            if(matcher.find()){
                hundredDigit = matcher.group();
            }

            regex = "[一|二|三|四|五|六|七|八|九]{1}十";
            pattern = Pattern.compile(regex);
            matcher = pattern.matcher(text);
            if(matcher.find()){
                tenDigit = matcher.group();
            }

            BigDecimal tempResult = null;

            if(StringUtils.isNotEmpty(hundredDigit)){
                hundredDigit = hundredDigit.substring(0,hundredDigit.indexOf("百"));
                logger.info("百位：{}",hundredDigit);
                BigDecimal hundredDecimal = new BigDecimal(dataMap.get(hundredDigit)).multiply(BigDecimal.valueOf(100));
                tempResult = hundredDecimal;
            }

            if(StringUtils.isNotEmpty(tenDigit)){
                tenDigit = tenDigit.substring(0,tenDigit.indexOf("十"));
                logger.info("十位：{}",tenDigit);
                BigDecimal tenDecimal = new BigDecimal(dataMap.get(tenDigit)).multiply(BigDecimal.valueOf(10));
                tempResult = tempResult.add(tenDecimal);
            }

            if(text.length()-1 != text.indexOf("十")){
                singleDigit = text.substring(text.indexOf("十")+1);
                logger.info("个位：{}",singleDigit);

                BigDecimal singleDecimal = new BigDecimal(dataMap.get(singleDigit));

                tempResult = tempResult.add(singleDecimal);
            }

            result = tempResult.stripTrailingZeros().toPlainString();
        }else{
            regex = "[一|二|三|四|五|六|七|八|九]百零[一|二|三|四|五|六|七|八|九]{1}";
            pattern = Pattern.compile(regex);
            matcher = pattern.matcher(text);
            if(matcher.matches()){
                //百位数
                String hundredDigit = "";
                //个位数
                String singleDigit = "";

                regex = "[一|二|三|四|五|六|七|八|九]{1}百";
                pattern = Pattern.compile(regex);
                matcher = pattern.matcher(text);
                if(matcher.find()){
                    hundredDigit = matcher.group();
                }

                hundredDigit = hundredDigit.substring(0,hundredDigit.indexOf("百"));
                logger.info("百位：{}",hundredDigit);
                singleDigit = text.substring(text.indexOf("零")+1);
                logger.info("个位：{}",hundredDigit);

                BigDecimal hundredDecimal = new BigDecimal(dataMap.get(hundredDigit)).multiply(BigDecimal.valueOf(100));
                BigDecimal singleDecimal = new BigDecimal(dataMap.get(singleDigit));

                BigDecimal tempResult = hundredDecimal.add(singleDecimal);

                result=tempResult.stripTrailingZeros().toPlainString();
            }else{
                regex = "[一|二|三|四|五|六|七|八|九]十[一|二|三|四|五|六|七|八|九]{0,1}";
                pattern = Pattern.compile(regex);
                matcher = pattern.matcher(text);

                if(matcher.matches()){
                    //十位数
                    String tenDigit = "";
                    //个位数
                    String singleDigit = "";

                    BigDecimal tempResult = null;

                    regex = "[一|二|三|四|五|六|七|八|九]{1}十";
                    pattern = Pattern.compile(regex);
                    matcher = pattern.matcher(text);
                    if(matcher.find()){
                        tenDigit = matcher.group();
                    }

                    if(StringUtils.isNotEmpty(tenDigit)){
                        tenDigit = tenDigit.substring(0,tenDigit.indexOf("十"));
                        logger.info("十位：{}",tenDigit);
                        BigDecimal tenDecimal = new BigDecimal(dataMap.get(tenDigit)).multiply(BigDecimal.valueOf(10));
                        tempResult = tenDecimal;
                    }

                    if(text.length()-1 != text.indexOf("十")){
                        singleDigit = text.substring(text.indexOf("十")+1);
                        logger.info("个位：{}",singleDigit);

                        BigDecimal singleDecimal = new BigDecimal(dataMap.get(singleDigit));

                        tempResult = tempResult.add(singleDecimal);
                    }

                    result = tempResult.stripTrailingZeros().toPlainString();
                }else{
                    regex = "十{1}[一|二|三|四|五|六|七|八|九]{1}";
                    pattern = Pattern.compile(regex);
                    matcher = pattern.matcher(text);

                    if(matcher.matches()){
                        //个位数
                        String singleDigit = text.substring(1);
                        logger.info("十位：十，个位：{}",singleDigit);

                        result = BigDecimal.TEN.add(BigDecimal.valueOf(dataMap.get(singleDigit))).stripTrailingZeros().toPlainString();
                    }else{
                        regex = "[一|二|三|四|五|六|七|八|九|十]{1}";
                        pattern = Pattern.compile(regex);
                        matcher = pattern.matcher(text);

                        if(matcher.matches()){
                            //个位数
                            result = String.valueOf(dataMap.get(text));
                        }
                    }

                }
            }
        }

        logger.info("数字转换，返回结果：{}",result);
        return result;
    }

    //获取发行期限
    public Map<String,String> getReleaseDate(String text){
        Map<String,String> map = new HashMap<>();

        try{
            logger.info("发行期限：{}",text);
            text = text.replaceAll("[。|\\s]{1,}","");
            logger.info("字符处理：{}",text);

            String regex = "\\d{0,}\\.{0,1}\\d{1,}年|[一|二|三|四|五|六|七|八|九|十|百]{1,}年";
            Pattern pattern = Pattern.compile(regex);
            Matcher matcher = pattern.matcher(text);

            String releaseDate = "";

            if(matcher.find()){
                releaseDate = matcher.group();
                logger.info("匹配数据：{}",releaseDate);
            }

            if(StringUtils.isNotEmpty(releaseDate)){
                regex = "\\d{0,}\\.{0,1}\\d{1,}年";
                pattern = Pattern.compile(regex);
                matcher = pattern.matcher(releaseDate);

                if(matcher.matches()){
                    map.put("flag","year");
                    map.put("value",releaseDate.substring(0,releaseDate.indexOf("年")));
                }else{
                    regex = "[一|二|三|四|五|六|七|八|九|十|百]{1,}年";
                    pattern = Pattern.compile(regex);
                    matcher = pattern.matcher(releaseDate);

                    if(matcher.matches()){
                        releaseDate = releaseDate.substring(0,releaseDate.indexOf("年"));

                        String releaseDateNum = convertNumber(releaseDate);

                        map.put("flag","year");
                        map.put("value",releaseDateNum);
                    }
                }
            }else{
                //如果不是年，则按天来匹配
                regex = "\\d{0,}\\.{0,1}\\d{1,}天|[一|二|三|四|五|六|七|八|九|十|百]{1,}天";
                pattern = Pattern.compile(regex);
                matcher = pattern.matcher(text);

                if(matcher.find()){
                    releaseDate = matcher.group();
                }

                if(StringUtils.isNotEmpty(releaseDate)){
                    regex = "\\d{0,}\\.{0,1}\\d{1,}天";
                    pattern = Pattern.compile(regex);
                    matcher = pattern.matcher(releaseDate);

                    if(matcher.matches()){
                        map.put("flag","day");
                        map.put("value",releaseDate.substring(0,releaseDate.indexOf("天")));
                    }else{
                        regex = "[一|二|三|四|五|六|七|八|九|十|百]{1,}天";
                        pattern = Pattern.compile(regex);
                        matcher = pattern.matcher(releaseDate);

                        if(matcher.matches()){
                            releaseDate = releaseDate.substring(0,releaseDate.indexOf("年"));

                            String releaseDateNum = convertNumber(releaseDate);

                            map.put("flag","day");
                            map.put("value",releaseDateNum);
                        }
                    }
                }
            }

        }catch (Exception e){
            logger.error(e.getMessage(),e);
        }

        if(MapUtils.isEmpty(map)){
            map.put("flag","false");
        }

        return map;
    }


    public Calendar dealDate(String text){
        try{
            text=text.replaceAll("[\\s|【|】|\\[|\\]|(|)|（|）]{1,}","");
            logger.info("处理时间：{}",text);

            //解析文本，匹配年份
            String yearRegex = "(\\d{1,}年)*";
            Pattern pattern = Pattern.compile(yearRegex);
            Matcher matcher = pattern.matcher(text);
            List<String> yearList = new ArrayList<>();

            while(matcher.find()) {
                if(StringUtils.isNotEmpty(matcher.group())){
                    yearList.add(matcher.group().substring(0,matcher.group().indexOf("年")));
                }
            }
            logger.info("匹配年份：{}",yearList);

            //解析文本，匹配月份
            String monthRegex = "(\\d{1,}月)*";
            List<String> monthList = new ArrayList<>();
            pattern = Pattern.compile(monthRegex);
            matcher = pattern.matcher(text);
            while(matcher.find()) {
                if(StringUtils.isNotEmpty(matcher.group())){
                    monthList.add(matcher.group().substring(0,matcher.group().indexOf("月")));
                }
            }
            logger.info("匹配月份：{}",monthList);

            //解析文本，匹配日期
            String dayRegex = "(\\d{1,}日)*";
            List<String> dayList = new ArrayList<>();
            pattern = Pattern.compile(dayRegex);
            matcher = pattern.matcher(text);
            while(matcher.find()) {
                if(StringUtils.isNotEmpty(matcher.group())){
                    dayList.add(matcher.group().substring(0,matcher.group().indexOf("日")));
                }
            }
            logger.info("匹配日期：{}",dayList);

            if(yearList.size()>0 && monthList.size()>0 && dayList.size()>0){
                String year=Collections.max(yearList);
                logger.info("年：{}",year);
                String month=Collections.max(monthList);
                logger.info("月：{}",month);
                String day=Collections.max(dayList);
                logger.info("日：{}",day);

                Calendar calendar = Calendar.getInstance();
                calendar.set(Calendar.YEAR,Integer.parseInt(year));

                if(Integer.valueOf(month)<10){
                    month = month.replace("0","");
                }
                calendar.set(Calendar.MONTH,Integer.valueOf(month)-1);

                if(Integer.valueOf(day)<10){
                    day = day.replace("0","");
                }
                calendar.set(Calendar.DATE,Integer.valueOf(day));

                return calendar;
            }else{
                return null;
            }

        }catch (Exception e){
            logger.error(e.getMessage(),e);
            return null;
        }
    }

}
